import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14

Page {
    id: deviceControlPage
    title: "设备控制"
    
    property var deviceStatus: ({})
    property var deviceParameters: ({})
    
    Component.onCompleted: {
        updateDeviceInfo()
    }
    
    Timer {
        id: updateTimer
        interval: 2000
        running: true
        repeat: true
        onTriggered: updateDeviceInfo()
    }
    
    function updateDeviceInfo() {
        if (typeof qmlBridge !== 'undefined') {
            deviceStatus = qmlBridge.getDeviceStatus()
            deviceParameters = qmlBridge.getDeviceParameters()
        }
    }
    
    function startDevice() {
        if (typeof qmlBridge !== 'undefined') {
            qmlBridge.startDevice()
        }
    }
    
    function stopDevice() {
        if (typeof qmlBridge !== 'undefined') {
            qmlBridge.stopDevice()
        }
    }
    
    function startSampling() {
        if (typeof qmlBridge !== 'undefined') {
            var mode = samplingModeComboBox.currentIndex
            var parameter = parameterSpinBox.value
            qmlBridge.startSampling(mode, parameter)
        }
    }
    
    function stopSampling() {
        if (typeof qmlBridge !== 'undefined') {
            qmlBridge.stopSampling()
        }
    }
    
    ScrollView {
        anchors.fill: parent
        anchors.margins: 20
        
        ColumnLayout {
            width: parent.width
            spacing: 20
            
            // 标题
            Text {
                text: "设备控制"
                font.pixelSize: 24
                font.bold: true
                Layout.fillWidth: true
            }
            
            // 设备状态组
            GroupBox {
                title: "设备状态"
                Layout.fillWidth: true
                
                GridLayout {
                    anchors.fill: parent
                    columns: 2
                    columnSpacing: 20
                    rowSpacing: 10
                    
                    Text {
                        text: "设备状态:"
                        font.bold: true
                    }
                    Text {
                        text: deviceStatus.statusText || "未知"
                        color: {
                            var status = deviceStatus.status || ""
                            if (status === "DEVICE_IDLE") return "green"
                            if (status === "DEVICE_SAMPLING") return "blue"
                            if (status === "DEVICE_ERROR") return "red"
                            return "black"
                        }
                    }
                    
                    Text {
                        text: "流量:"
                        font.bold: true
                    }
                    Text {
                        text: (deviceParameters.flowRate || 0) + " L/min"
                    }
                    
                    Text {
                        text: "电池电量:"
                        font.bold: true
                    }
                    Text {
                        text: (deviceParameters.batteryLevel || 0) + "%"
                        color: {
                            var level = deviceParameters.batteryLevel || 0
                            if (level > 50) return "green"
                            if (level > 20) return "orange"
                            return "red"
                        }
                    }
                    
                    Text {
                        text: "风扇转速:"
                        font.bold: true
                    }
                    Text {
                        text: (deviceParameters.fanSpeed || 0) + " RPM"
                    }
                    
                    Text {
                        text: "温度:"
                        font.bold: true
                    }
                    Text {
                        text: (deviceParameters.temperature || 0) + "°C"
                    }
                    
                    Text {
                        text: "湿度:"
                        font.bold: true
                    }
                    Text {
                        text: (deviceParameters.humidity || 0) + "%"
                    }
                }
            }
            
            // 设备控制组
            GroupBox {
                title: "设备控制"
                Layout.fillWidth: true
                
                ColumnLayout {
                    anchors.fill: parent
                    spacing: 15
                    
                    RowLayout {
                        Button {
                            text: "启动设备"
                            enabled: deviceStatus.status !== "DEVICE_SAMPLING"
                            onClicked: startDevice()
                        }
                        
                        Button {
                            text: "停止设备"
                            enabled: deviceStatus.status !== "DEVICE_IDLE"
                            onClicked: stopDevice()
                        }
                    }
                    
                    Rectangle {
                        Layout.fillWidth: true
                        height: 1
                        color: "#cccccc"
                    }
                    
                    Text {
                        text: "采样控制"
                        font.bold: true
                        font.pixelSize: 16
                    }
                    
                    RowLayout {
                        Text {
                            text: "采样模式:"
                        }
                        ComboBox {
                            id: samplingModeComboBox
                            model: ["定时采样", "定量采样"]
                            Layout.preferredWidth: 150
                        }
                    }
                    
                    RowLayout {
                        Text {
                            text: samplingModeComboBox.currentIndex === 0 ? "采样时间 (秒):" : "采样体积 (L):"
                        }
                        SpinBox {
                            id: parameterSpinBox
                            from: samplingModeComboBox.currentIndex === 0 ? 10 : 1
                            to: samplingModeComboBox.currentIndex === 0 ? 3600 : 1000
                            value: samplingModeComboBox.currentIndex === 0 ? 300 : 100
                        }
                    }
                    
                    RowLayout {
                        Button {
                            text: "开始采样"
                            highlighted: true
                            enabled: deviceStatus.status === "DEVICE_IDLE"
                            onClicked: startSampling()
                        }
                        
                        Button {
                            text: "停止采样"
                            enabled: deviceStatus.status === "DEVICE_SAMPLING"
                            onClicked: stopSampling()
                        }
                    }
                }
            }
            
            // 最近错误信息
            GroupBox {
                title: "系统信息"
                Layout.fillWidth: true
                visible: deviceStatus.lastError && deviceStatus.lastError !== ""
                
                Text {
                    anchors.fill: parent
                    text: deviceStatus.lastError || ""
                    color: "red"
                    wrapMode: Text.WordWrap
                }
            }
            
            Item {
                Layout.fillHeight: true
            }
        }
    }
}